/*
 * WorkTimer.java
 *
 * Created on July 13, 2006, 9:21 AM
 *
 */

package org.vcboard;

import java.awt.*;
import java.io.FileInputStream;
import java.util.*;
import java.util.prefs.*;
import javax.swing.*;
import org.apache.log4j.*;
import org.vcboard.database.*;
import org.vcboard.event.*;
import org.vcboard.model.*;
import org.vcboard.view.*;

/**
 * @author Josh
 */
public class WorkTimer
{
   private static Logger log = Logger.getLogger(WorkTimer.class);
   private static WorkTimer app = null;
   private static WorkTimerGUI gui = null;
   private static Authorize auth = null;
   private static SetPeriod periodFrame = null;
   private static ModifyLog modifyLog = null;
   private static AppPrefs dbInfo = null;
   
   private static User user = null;
   private static LogDate logPeriod = null;
   private static Preferences prefs = null;
   private static Properties defaultProps = null;
   
   private static Vector<UserChangedListener> userChangedListeners = new Vector<UserChangedListener>();
   private static Vector<LogsUpdatedListener> logsUpdatedListener = new Vector<LogsUpdatedListener>();
   private static Vector<TimeLog> timeLogs = null;
   
   private static long timeout = -1;
   
   /** Creates a new instance of WorkTimer */
   public WorkTimer()
   {
      WorkTimerGUI.getInstance().setVisible(true);
   }
   
   public static void resetTimeout()
   {
      timeout = 0;
   }
   
   public static long getTimeout()
   {
      return timeout;
   }
   
   public static void addToTimeout()
   {
      timeout++;
   }
   
   /**
    * @param args the command line arguments
    */
   public static void main(String[] args)
   {
      app = new WorkTimer();
   }
   
   public static void updateLogs()
   {
      setTimeLogs(MySQLDbIf.getInstance().getLogs());
   }
   
   public static Vector<TimeLog> getLogs()
   {
      if (timeLogs == null)
      {
         setTimeLogs(new Vector<TimeLog>());
         updateLogs();
      }
      
      return timeLogs;
   }
   
   public static void authorize()
   {
      setAuth(new Authorize());
      getAuth().setVisible(true);
      WorkTimerGUI.getInstance().toggleOptions();
   }
   
   public static void addListener(LogsUpdatedListener listener)
   {
      logsUpdatedListener.add(listener);
   }
   
   public static void fireLogsChanged()
   {
      updateLogs();
      resetTimeout();
      
      WorkTimerGUI.getInstance().timerUpdate();
      
      for (LogsUpdatedListener l : logsUpdatedListener)
      {
         l.updateLogs();
      }
   }
   
   public static void addListener(UserChangedListener listener)
   {
      userChangedListeners.add(listener);
   }
   
   public static void fireUserChanged()
   {
      for (UserChangedListener l : userChangedListeners)
      {
         l.userChanged();
      }
    
      updateLogs();
      resetTimeout();
   }
   
   public static User getUser()
   {
      if (user == null)
      {
         user = new User();
      }
      
      return user;
   }
   
   public static SetPeriod getSetPeriod()
   {
      if (periodFrame == null)
      {
         periodFrame = new SetPeriod(null, true);
      }
      
      return periodFrame;
   }
   
   public static void setUser(String username, String password)
   {
      user = MySQLDbIf.getInstance().logIn(username, password);
      
      if (getUser().getUserId() == -1)
      {
         authorize();
      }
      
      getProperties();
      fireLogsChanged();
      fireUserChanged();
   }
   
   public static void setUser(User aUser)
   {
      user = aUser;
      fireUserChanged();
   }
   
   public static Authorize getAuth()
   {
      if (auth == null)
      {
         auth = new Authorize();
      }
      resetTimeout();
      
      return auth;
   }
   
   public static void setAuth(Authorize aAuth)
   {
      auth = aAuth;
   }
   
   public static void centerForm(JFrame frm)
   {
      resetTimeout();
      Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
      int x = (int) Math.round((dim.getWidth() - frm.getWidth()) / 2);
      int y = (int) Math.round((dim.getHeight() - frm.getHeight()) / 2);
      frm.setLocation(x, y);
   }
   
   public static void centerForm(JDialog frm)
   {
      Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
      int x = (int) Math.round((dim.getWidth() - frm.getWidth()) / 2);
      int y = (int) Math.round((dim.getHeight() - frm.getHeight()) / 2);
      frm.setLocation(x, y);
   }

   public static LogDate getLogPeriod()
   {
      return logPeriod;
   }

   public static void setLogPeriod(LogDate aLogPeriod)
   {
      logPeriod = aLogPeriod;
   }

   public static void setTimeLogs(Vector<TimeLog> aTimeLogs)
   {
      timeLogs = aTimeLogs;
   }

   public static ModifyLog getModifyLog()
   {
      if (modifyLog == null)
      {
         modifyLog = new ModifyLog();
      }
      resetTimeout();
      
      return modifyLog;
   }

   public static void setModifyLog(ModifyLog aModifyLog)
   {
      modifyLog = aModifyLog;
   }
   
   public static Properties getProperties()
   {
      if (defaultProps == null)
      {
         defaultProps = new Properties();
         
         try
         {
            FileInputStream in = new FileInputStream("settings.ini");
            defaultProps.load(in);
            in.close();
         }
         catch (Exception e)
         {
            log.error("Failed to load application preferences");
         }
      }
      
      return defaultProps;
   }

   public static AppPrefs getDbInfo()
   {
      if (dbInfo == null)
      {
         dbInfo = new AppPrefs();
      }
      resetTimeout();
      
      return dbInfo;
   }
}
